package com.uenpay.im.ui.old.activity.dynamic;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.qyx.android.message.type.PacketType;
import com.qyx.android.weight.edittext.Emoji;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2015-12-17 13:45:09
 * @copyRight aiti
 * @function
 */
public class DynamicHistoryAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<Reply> arrayList = new ArrayList<Reply>();
	private Emoji mEmoji;
	private Context mContext;
	
	public DynamicHistoryAdapter(Context _Context, ArrayList<Reply> _ArrayList) {
		arrayList = _ArrayList;
		mContext  = _Context;
		mInflater = LayoutInflater.from(_Context);
		this.mEmoji = new Emoji();
	}
	public void setData(ArrayList<Reply> _ArrayList) {
		arrayList = _ArrayList;
		notifyDataSetChanged();
	}
	
	public void addData(ArrayList<Reply> _ArrayList) {
		arrayList.addAll(_ArrayList);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList == null ? null : arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		DynamicHistoryViewHodler hodler = null;
		Reply reply = arrayList.get(arg0);
		if (arg1 == null) {
			hodler = new DynamicHistoryViewHodler();
			arg1 = mInflater
					.inflate(R.layout.item_dynamic_history_layout, null);
			hodler.reply_content_tv = (TextView) arg1
					.findViewById(R.id.reply_content_tv);
			hodler.reply_name_tv = (TextView) arg1
					.findViewById(R.id.reply_name_tv);
			hodler.create_time_tv = (TextView) arg1
					.findViewById(R.id.create_time_tv);
			hodler.send_content_tv = (TextView) arg1
					.findViewById(R.id.send_content_tv);
			hodler.reply_like_iv = (ImageView) arg1
					.findViewById(R.id.reply_like_iv);
			hodler.user_avatar = (RoundImageView) arg1
					.findViewById(R.id.user_avatar);
			hodler.last_iv = (MaskImageView) arg1.findViewById(R.id.last_iv);
			arg1.setTag(hodler);
		} else {
			hodler = (DynamicHistoryViewHodler) arg1.getTag();
		}
		if (reply.type == 2) {
			// like
			hodler.reply_content_tv.setVisibility(View.GONE);
			hodler.reply_like_iv.setVisibility(View.VISIBLE);
		} else {
			// reply
			hodler.reply_content_tv.setText(mEmoji
					.getSpannedRate(reply.content, mContext, 3));
			hodler.reply_content_tv.setVisibility(View.VISIBLE);
			hodler.reply_like_iv.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(reply.d_content)) {
			hodler.send_content_tv.setText(mEmoji
					.getSpannedRate(reply.d_content, mContext, 3));
			hodler.send_content_tv.setVisibility(View.VISIBLE);
			hodler.last_iv.setVisibility(View.GONE);
		} else {
			hodler.last_iv.setVisibility(View.VISIBLE);
			hodler.send_content_tv.setVisibility(View.GONE);
			hodler.last_iv.SetUrl(APIConfiguration.getFileDownloadPath(
					reply.fileMessage.fileid, reply.fileMessage.filehash));
		}
		hodler.reply_name_tv.setText(reply.nickname);
		hodler.create_time_tv.setText(reply.createtime);
		hodler.user_avatar.SetUrl(APIConfiguration.getAvatarUrlNormal(
				reply.custid + "", PacketType.PERSONAL));
		return arg1;
	}

	private class DynamicHistoryViewHodler {
		private TextView reply_name_tv, reply_content_tv, create_time_tv,
				send_content_tv;
		private ImageView reply_like_iv;
		private MaskImageView  last_iv;
		private RoundImageView user_avatar;
	}

}
